package cloudspace.vm.io.console;

import java.util.Iterator;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Menu;
import org.zkoss.zul.Menubar;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.Menupopup;


/**
 * The Hbox that represents all of the different visibility options for the
 * console vbox.
 */
public class VisToolbar extends Menubar
{
    /*
     * public class AutoScrollHandler implements EventListener { ConsoleLines
     * consoleBox; public AutoScrollHandler(ConsoleLines lines) { consoleBox =
     * lines; } public void onEvent(Event event) throws Exception { Menuitem
     * eventItem = (Menuitem) event.getTarget();
     * eventItem.setChecked(!eventItem.isChecked());
     * consoleBox.notifyAutoScrollChange(); } }
     */
    /**
     * The Class TimeVisChangeHandler. This handles a change to time stamp
     * visibility.
     */
    public class TimeVisChangeHandler implements EventListener
    {

        /** The console box. */
        ConsoleLines consoleBox;


        /**
         * Instantiates a new time vis change handler.
         * 
         * @param fellow
         *            the master Console Vertical Box
         */
        public TimeVisChangeHandler( ConsoleLines fellow )
        {
            consoleBox = fellow;
        }


        /*
         * (non-Javadoc)
         * 
         * @see
         * org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event
         * .Event)
         */
        public void onEvent( Event event ) throws Exception
        {
            Menuitem eventItem = (Menuitem)event.getTarget();
            eventItem.setChecked( !eventItem.isChecked() );
            consoleBox.notifyTimeStampChange();

        }

    }


    /**
     * The Class VisChangeHandler. This handles a change in visibility of a
     * stream.
     */
    public class VisChangeHandler implements EventListener
    {

        /** The Console Vbox parent */
        ConsoleLines conComp;


        /**
         * Instantiates a new vis change handler.
         * 
         * @param consoleComponent
         *            the console component
         */
        public VisChangeHandler( ConsoleLines consoleComponent )
        {
            conComp = consoleComponent;
        }


        /*
         * (non-Javadoc)
         * 
         * @see
         * org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event
         * .Event)
         */
        public void onEvent( Event event ) throws Exception
        {
            Menuitem eventItem = (Menuitem)event.getTarget();
            eventItem.setChecked( !eventItem.isChecked() );
            boolean isChecked = eventItem.isChecked();
            conComp.notifyVisChange( eventItem.getLabel(), isChecked );
        }

    }


    public class DistributedVisListener implements EventListener
    {

        /** The Console Vbox parent */
        ConsoleLines conComp;


        /**
         * Instantiates a new vis change handler.
         * 
         * @param consoleComponent
         *            the console component
         */
        public DistributedVisListener( ConsoleLines consoleComponent )
        {
            conComp = consoleComponent;
        }


        /*
         * (non-Javadoc)
         * 
         * @see
         * org.zkoss.zk.ui.event.EventListener#onEvent(org.zkoss.zk.ui.event
         * .Event)
         */
        public void onEvent( Event event ) throws Exception
        {
            Menuitem eventItem = (Menuitem)event.getTarget();
            eventItem.setChecked( !eventItem.isChecked() );
            conComp.notifyDisVisChange( ( (Menuitem)event.getTarget() ).isChecked() );
        }

    }

    /** For Serialization. */
    private static final long serialVersionUID = -2117673001259154623L;


    /**
     * This is notification from the console vbox that the stream provider has
     * been updated or created. It is used when a new streamID is added.
     * 
     * @param evt
     *            The event
     */
    public void onUpdate( Event evt )
    {
        ConsoleLines lines = (ConsoleLines)getFellow( "consoleLines" );
        Menu vis = new Menu( "Visibility" );
        Menu options = new Menu( "Options" );
        this.appendChild( vis );
        this.appendChild( options );
        Menupopup visPop = new Menupopup();
        Menupopup optPop = new Menupopup();
        vis.appendChild( visPop );
        options.appendChild( optPop );
        Console sp = (Console)evt.getData();
        Iterator<String> idIter = sp.getStreamIDs();
        while ( idIter.hasNext() )
        {
            String streamID = idIter.next();
            boolean visCheck = lines.isVisible( streamID );
            createOption( visPop,
                streamID,
                new VisChangeHandler( lines ),
                visCheck );
        }
        createOption( optPop,
            "Hide Time stamp",
            new TimeVisChangeHandler( lines ),
            !lines.isTimeStamp() );
        createOption( optPop,
            "Show All Prints",
            new DistributedVisListener( lines ),
            lines.isHistoryVisible() );
        this.invalidate();
    }


    // private String getMacroDynamicProperty(String propertyName)
    // {
    // String PropertyValue = (String) ((HtmlMacroComponent)
    // getParent().getParent())
    // .getDynamicProperty(propertyName);
    // return PropertyValue;
    // }

    private void createOption(
        Menupopup optPop,
        String optionName,
        EventListener el,
        boolean isChecked )
    {
        Menuitem newItem = new Menuitem( optionName );
        newItem.setId( "_CSUI" + optionName );
        newItem.addEventListener( "onClick", el );
        newItem.setChecked( isChecked );
        optPop.appendChild( newItem );
    }
}
